package com.leyou.page.listener;

import com.leyou.common.constants.MQConstants;
import com.leyou.page.service.PageService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 商品上下架监听器
 */
@Component
public class ItemListener {
    @Autowired
    private PageService pageService;

    /**
     * 上架->创建商品详情静态页
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = MQConstants.Queue.PAGE_ITEM_UP),
                    exchange = @Exchange(name = MQConstants.Exchange.ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
                    key = MQConstants.RoutingKey.ITEM_UP_KEY
            )
    )
    public void createStaticPage(Long spuId){
        pageService.createStaticPage(spuId);
    }


    /**
     * 下架->删除商品详情静态页
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = MQConstants.Queue.PAGE_ITEM_DOWN),
                    exchange = @Exchange(name = MQConstants.Exchange.ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
                    key = MQConstants.RoutingKey.ITEM_DOWN_KEY
            )
    )
    public void deleteStaticPage(Long spuId){
        pageService.deleteStaticPage(spuId);
    }

}
